/**
 * 
 */
package fr.fermeManager.library.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import fr.fermeManager.library.model.beans.TypeTransaction;

/**
 * @author Mo
 * 
 */
public class DaoTypeTransactionImplDerby implements DaoTypeTransaction {
	
	private SqlSession session = null;

	@Override
	public List<TypeTransaction> getListeTypesTransactions(int idFerme,boolean isAdmin) throws Exception {
		List<TypeTransaction> listeTypesTransactions = new ArrayList<TypeTransaction>();

		try {
			
			Map<String,Object> parameters = new HashMap<String, Object>();
			
			parameters.put("idFerme", idFerme);
			parameters.put("isAdmin", isAdmin);
			
			listeTypesTransactions = session.selectList("selectTypesTransactions",parameters);
			
		} catch (Exception e) {
			session.rollback();
			throw e;
		}

		return listeTypesTransactions;
	}

	@Override
	public TypeTransaction ajouterTypeTransaction(int idFerme, TypeTransaction nouveauTypeTransac) throws Exception {
		
		try {
			
			Map<String,Object> parameters = new HashMap<String, Object>();
			
			parameters.put("idFerme", idFerme);
			parameters.put("nouveauTypeTransac", nouveauTypeTransac);
			
			session.insert("ajouterTypeTransaction",parameters);
			
			session.commit();
			
			return nouveauTypeTransac;
			
		} catch (Exception e) {
			session.rollback();
			throw e;
		}
	}

	@Override
	public void modifierTypeTransaction(TypeTransaction typeTransactionSelectionnee) throws Exception {
	try {
			
			session.update("modifierTypeTransaction",typeTransactionSelectionnee);
			
			session.commit();
			
		} catch (Exception e) {
			session.rollback();
			throw e;
		}
		
	}

	@Override
	public void supprimerTypeTransaction(int idTypeTransaction) throws Exception {
		try {
			
			session.delete("effacerTypeTransaction",idTypeTransaction);
			
			session.commit();
			
		} catch (Exception e) {
			session.rollback();
			throw e;
		}
	}
	
	@Override
	public boolean existeTypeTransactionsActifsForFerme(int idFerme) throws Exception {
		
		int nbActifs =0;
		
		try {
			
			nbActifs = session.selectOne("existeTypeTransactionsActifsForFerme",idFerme);
			
		} catch (Exception e) {
			session.rollback();
			throw e;
		}
		return (nbActifs>0);
	}

	@Override
	public void setSession(SqlSession sessionParam) throws Exception {
		session = sessionParam;
	}

	
}
